
/**
 * Date:2022年8月1日下午8:14:24
 * Copyright (c) 2022, www.bwbroad.com All Rights Reserved.
 *
*/

package com.pojo;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * Description: TODO <br/>
 * Date:     2022年8月1日 下午8:14:24 <br/>
 * @author   wangbing	 
 */

public class Page<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	public long count; // 总行数
	public int pageSize = 2; // 每页显示的行数
	public int pageNo = 1; // 当前页号
	public int totalPage; // 总页数
	private List<T> list;
	private boolean isFirst;
	private boolean isLast;
	private int pageWindowSize = 6;

	public Page() {
	}

	public Page(int count) {
		this(count, 10);
	}

	public Page(int pageNo, int pageSize) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}

	public Page(Page<T> pager) {
		this.count = pager.count; // 总行数
		this.pageSize = pager.pageSize; // 每页显示的行数
		this.pageNo = pager.pageNo; // 当前页号
		this.totalPage = pager.totalPage; // 总页数
		this.list = pager.list;
	}

	public int getTotalPage() {
		pageSize = pageSize > 0 ? pageSize : 10;
		totalPage = Long.valueOf(count / pageSize).intValue();
		long mod = count % pageSize;
		if (mod > 0) {
			totalPage++;
		}
		return totalPage;
	}

	public int getStart() {
		return (pageNo - 1) * pageSize;
	}

	public long getCount() {
		return count;
	}

	public Page<T> setCount(long count) {
		getTotalPage();
		this.count = count;
		return this;
	}

	public int getPageSize() {
		return pageSize;
	}

	public Page<T> setPageSize(int pageSize) {
		this.pageSize = pageSize;
		return this;
	}

	public int getpageNo() {
		return pageNo;
	}

	public Page<T> setpageNo(int pageNo) {
		this.pageNo = pageNo;
		return this;
	}

	public List<T> getList() {
		return list;
	}

	public Page<T> setList(List<T> list) {
		this.list = list;
		return this;
	}

	public boolean getIsFirst() {
		this.isFirst = this.pageNo == 1;
		return isFirst;
	}

	public boolean getIsLast() {
		this.isLast = this.pageNo == this.totalPage;
		return isLast;
	}

	public List<Integer> getPageWindow() {
		// 螺旋算法,以center 为中心
		int center = this.pageNo;
		var newW = new LinkedList<Integer>();
		newW.add(center);
		var len = newW.size();
		for (int i = 1; i < this.pageWindowSize; i++) {
			// 下一页
			int next = center + i;
			if (next <= this.totalPage) {
				if (len < this.pageWindowSize) {
					newW.addLast(next);// 添加到尾部
					len = newW.size();
				} else {
					break;
				}
			}
			// 上一页
			int pre = center - i;
			if (pre > 0) {
				if (len < this.pageWindowSize) {
					newW.addFirst(pre);// 添加到头部
					len = newW.size();
				} else {
					break;
				}
			}
		}
		return newW;
	}

}

